package daily;

import java.util.Arrays;
import java.util.List;

class Solution3128 {

    public static void main(String[] args) {
        Solution3128 solution = new Solution3128();
        long result = solution.numberOfRightTriangles(solution.case1());
        System.out.println(result);

        result = solution.numberOfRightTriangles(solution.case2());
        System.out.println(result);

        result = solution.numberOfRightTriangles(solution.case3());
        System.out.println(result);
    }

    public long numberOfRightTriangles(int[][] grid) {
        int rowLength = grid.length;
        int columnLength = grid[0].length;
        int[] columnSum = new int[columnLength];
        for (int row = 0; row < rowLength; row++) {
            for (int col = 0; col < columnLength; col++) {
                columnSum[col] += grid[row][col];
            }
        }

        long result = 0L;
        for (int row = 0; row < rowLength; row++) {
            int rowSum = Arrays.stream(grid[row]).sum();
            for (int col = 0; col < columnLength; col++) {
                if (grid[row][col] == 1) {
                    result += (rowSum-1) * (columnSum[col]-1);
                }
            }
        }
        return result;
    }

    private int[][] case1() {
        return new int[][] {
                {0, 1, 0},
                {0, 1, 1},
                {0, 1, 0}
        };
    }

    private int[][] case2() {
        return new int[][] {
                {1,0,0,0},
                {0,1,0,1},
                {1,0,0,0}
        };
    }

    private int[][] case3() {
        return new int[][] {
                {1,0,1},
                {1,0,0},
                {1,0,0}
        };
    }
}
